//
//  YZMRotationInforView.m
//  YZMicroStock
//
//  Created by apple on 17/4/5.
//  Copyright © 2017年 cqjr. All rights reserved.
//

#import "YZMRotationInforView.h"
#import <BlocksKit/UIControl+BlocksKit.h>


@interface YZMRotationInforView()

@property (nonatomic, strong) UILabel *nameLB;
@property (nonatomic, strong) UILabel *codeLB;
@property (nonatomic, strong) UILabel *valueLB;
@property (nonatomic, strong) UILabel *changeLB;
@property (nonatomic, strong) UILabel *highRiskLB;
@property (nonatomic, strong) UIButton *disMissBtn;

@end

@implementation YZMRotationInforView

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        self.userInteractionEnabled = YES;
        self.backgroundColor = [YZMAppStyle colorForMain];
        [self createObjects];
    }
    
    return self;
}


- (void)updateWithStockCode:(NSString *)stockCode andStockName:(NSString *)stockName{
    
    self.nameLB.text = stockName;
    self.codeLB.text = stockCode;
    self.valueLB.text = @"--";
    self.changeLB.text = @"--   --";
    self.highRiskLB.hidden = YES;
    
    self.valueLB.textColor = [YZMAppStyle colorForGrayText];
    self.changeLB.textColor = [YZMAppStyle colorForGrayText];
}


- (void)setRealStock:(YZMStockRealModel *)realStock{
    
    _realStock = realStock;
    self.nameLB.text = realStock.name;
    if ( [@(realStock.price) isEqualWithDouble:0.0] ) {
        
        self.valueLB.text = @(realStock.lastClosePrice).p02fString;
        self.highRiskLB.hidden = NO;
        self.highRiskLB.text = @"停牌股";
        return;
    }
    
    self.valueLB.text = @(realStock.price).p02fString;
    double distance = realStock.price - realStock.lastClosePrice;
    if ([@(distance) isEqualWithDouble:0.0]) {
        
        self.valueLB.textColor = [YZMAppStyle colorForGrayText];
        self.changeLB.textColor = [YZMAppStyle colorForGrayText];
        self.changeLB.text = @"+0.00   +0.00%";
        
    }else if([@(distance) isGreaterThanDouble:0.0]){
        
        self.valueLB.textColor = [YZMAppStyle colorForRiseText];
        self.changeLB.textColor = [YZMAppStyle colorForRiseText];
        self.changeLB.text = [NSString stringWithFormat:@"+%@   +%@%%",@(distance).p02fString, @(distance*100/realStock.lastClosePrice).p02fString];
        
    }else{
        self.valueLB.textColor = [YZMAppStyle colorForDropText];
        self.changeLB.textColor = [YZMAppStyle colorForDropText];
        self.changeLB.text = [NSString stringWithFormat:@"%@   %@%%",@(distance).p02fString, @(distance*100/realStock.lastClosePrice).p02fString];
    }
    
    self.highRiskLB.hidden = ![[YZMConfigureService sharedService] isInLimitStockListWithStockCode:realStock.code];
    self.highRiskLB.text = @"风险股";
}

- (void)formatColorWithLabel:(UILabel *)label andGreateDouble:(double)greaterDouble andSmallDouble:(double)smallDouble{
    
    if ( [@(greaterDouble) isGreaterThanDouble:smallDouble] ) {
        
        label.textColor = [YZMAppStyle colorForRiseText];
        
    }else if( [@(greaterDouble) isEqualWithDouble:smallDouble] ){
        
        label.textColor = [YZMAppStyle colorForGrayText];
        
    }else{
        
        label.textColor = [YZMAppStyle colorForDropText];
    }
}

#pragma mark -- ui

- (void)createObjects{
    
    //股票名称
    self.nameLB = [YZMAppStyle labelWithFont:[YZMAppStyle fontWithSize:18] andColor:[YZMAppStyle colorForWhite]];
    self.nameLB.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.nameLB];
    [self.nameLB mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self).offset(20);
        make.top.equalTo(self).offset(15);
    }];

    //股票代码
    self.codeLB = [YZMAppStyle labelWithFont:[YZMAppStyle mediumSizeFont] andColor:[YZMAppStyle colorForLightGrayText]];
    [self addSubview:self.codeLB];
    [self.codeLB mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.nameLB.mas_right).offset(6);
        make.centerY.equalTo(self.nameLB);
    }];
    
    //风险股停牌股提醒
    self.highRiskLB = [UILabel new];
    self.highRiskLB.textColor = [YZMAppStyle colorForMain];
    self.highRiskLB.font = [YZMAppStyle smallSizeFont];
    self.highRiskLB.backgroundColor = [YZMAppStyle colorForGrayUnable];
    self.highRiskLB.layer.cornerRadius = 1;
    self.highRiskLB.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.highRiskLB];
    [self.highRiskLB mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(self.codeLB.mas_right).offset(3);
        make.centerY.equalTo(self.codeLB);
    }];
    
    //股票价格
    self.valueLB = [YZMAppStyle labelWithFont:[YZMAppStyle fontWithSize:18] andColor:[YZMAppStyle colorForRiseText]];
    [self addSubview:self.valueLB];
    [self.valueLB mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.equalTo(self.codeLB.mas_right).offset(62.0/667*ScreenHeight);
        make.centerY.equalTo(self.nameLB);
    }];
    
    //股票增量
    self.changeLB = [YZMAppStyle labelWithFont:[YZMAppStyle mediumSizeFont] andColor:[YZMAppStyle colorForRiseText]];
    [self addSubview:self.changeLB];
    [self.changeLB mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.valueLB.mas_right).offset(30.0/667*ScreenHeight);
        make.centerY.equalTo(self.codeLB);
    }];
    
    self.disMissBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.disMissBtn setImage:[[UIImage imageNamed:@"icon_rotationVC_end"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [self.disMissBtn bk_addEventHandler:^(id sender) {
        if (self.dismissBlock) {
            self.dismissBlock();
        }
    } forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.disMissBtn];
    [self.disMissBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(self.mas_right).offset(-21);
        make.centerY.equalTo(self.codeLB);
        make.width.height.mas_equalTo(44);
    }];
    
    self.timeLB =[YZMAppStyle labelWithMediumWhite];
    [self addSubview:self.timeLB];
    [self.timeLB mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.changeLB.mas_right).offset(63.0/667*ScreenHeight);
        make.centerY.equalTo(self.codeLB);
    }];
    self.timeLB.text = @"-- --";

    self.nameLB.text = @"--";
    self.codeLB.text = @"--";
    
    self.valueLB.text = @"--";
    self.changeLB.text = @"--   --";
}

@end
